//
//  SCNUIImageExtension.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/11/20.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import Foundation
import UIKit

extension UIImage {
    /**
     *  生成颜色图片
     */
    func getImageWithColor(_ size: CGSize,color: UIColor) -> UIImage {
        
        let rect: CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        
        UIGraphicsBeginImageContext(rect.size)
        
        let context: CGContext = UIGraphicsGetCurrentContext()!
        
        context.setFillColor(color.cgColor)
        
        context.fill(rect)
        
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsGetCurrentContext()
        
        return image!
        

    }
    
    /**
     *  对图片尺寸进行压缩
     */
    func imageScaledToSize(_ image:UIImage,newSize:CGSize)->UIImage{
        
        UIGraphicsBeginImageContext(newSize)
        image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage!
        
    }
}
